﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LeMigue.TeamPortal.Base.Biz.Interface;
using LeMigue.TeamPortal.Base.View;
using LeMigue.Scrum.Iterface.Model;
using LeMigue.Scrum.Iterface.Enum;
using System.Collections.ObjectModel;

namespace LeMigue.TeamPortal.Dev.Biz
{
    public class DevTaskBiz : ITaskBiz
    {
        private IList<Task> _tasks = new List<Task>();

        public DevTaskBiz(){
            Task task1 = new Task(1, "Task 01");
            task1.State = TaskState.ToDo;

            _tasks.Add(task1);
        }


        public IList<Task> Tasks
        {
            get
            {
                return new ReadOnlyCollection<Task>(_tasks);
            }
        }
        public TaskView Load(int? id)
        {
            throw new NotImplementedException();
        }

        public Task Save(Task task)
        {
            throw new NotImplementedException();
        }

        public IList<TaskView> List(IList<Task> tasks, TaskState? state)
        {
            if (tasks == null)
                return new List<TaskView>();
            if (state.HasValue)
                //Se tiver algum filho com este status eu trago a tarefa independente do status
                return tasks.Where(i => i.State == state.Value || HasSubState(i, state.Value)).Select(i => new TaskView(i)).ToList();
            else
                return tasks.Select(i => new TaskView(i)).ToList();
        }
        public bool HasSubState(Task task, TaskState state)
        {
            if (task.HasTask)
            {
                return task.Tasks.Where(t => t.State == state || HasSubState(t, state)).Count() > 0;
            }
            return false;
        }

        public IList<TaskView> List(Sprint sprint, TaskState? state)
        {
            return List(sprint.Tasks, state);
        }

        public IList<TaskView> List(Bug bug, TaskState? state)
        {
            return List(bug.Tasks, state);
        }

        public IList<TaskView> List(ItemBacklog backlog, TaskState? state)
        {
            return List(backlog.Tasks, state);
        }

        public IList<TaskView> List(IHasTask item, TaskState? state)
        {
            return List(item.Tasks, state);
        }

        public TaskView New(int? idParent)
        {
            throw new NotImplementedException();
        }

        public Task Create(Task task, Iteration iteracao, Team team)
        {
            throw new NotImplementedException();
        }
    }
}
